<?php
class PhanquyensController extends AppController {

	var $name = 'Phanquyens';
	var $uses = array('Phanquyen','User','Tgiachidoan','Tgiachidoancb','Chucvusv','Role','Chucv','Chucvucb');
	
	function index() {
		$this->Phanquyen->recursive = 0;
		$this->set('phanquyens', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phanquyen', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phanquyen', $this->Phanquyen->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phanquyen->create();
			if ($this->Phanquyen->save($this->data)) {
				$this->Session->setFlash(__('The phanquyen has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanquyen could not be saved. Please, try again.', true));
			}
		}
		$khoas = $this->Phanquyen->Khoa->find('list');
		$chucvs = $this->Phanquyen->Chucv->find('list');
		$roles = $this->Phanquyen->Role->find('list');
		$this->set(compact('khoas', 'chucvs', 'roles'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid phanquyen', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phanquyen->save($this->data)) {
				$this->Session->setFlash(__('The phanquyen has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phanquyen could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phanquyen->read(null, $id);
		}
		$khoas = $this->Phanquyen->Khoa->find('list');
		$chucvs = $this->Phanquyen->Chucv->find('list');
		$roles = $this->Phanquyen->Role->find('list');
		$this->set(compact('khoas', 'chucvs', 'roles'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for phanquyen', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phanquyen->delete($id)) {
			$this->Session->setFlash(__('Phanquyen deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Phanquyen was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function dsuser($role_id=null)
	{
		if(!$role_id)
			$this->redirect(array('controller'=>'roles','action'=>'index'));
			
		if(!empty($this->data))
		{
			$err=0;
			$record['Phanquyen']['role_id']=$role_id;
			foreach ($this->data['Phanquyen']['doanvien'] as $mso =>$value)
			{
				if($value==1)
				{
					$user = $this->User->find('first',array('conditions'=>array('username'=>$mso)));
					$record['Phanquyen']['user_id']=$user['User']['id'];
					if(!$this->Phanquyen->save($record))
					{$err=1;}
				}
			}
			if($err==1)
				$this->Session->setFlash('Lỗi khi lưu','error');
			else 
				$this->Session->setFlash('Thêm quyền thành công','success');
			$this->redirect(array('action'=>'dsuser',$role_id));
		}
		$khoa_id = $this->Session->read('khoa_id');
		$maso = $this->Phanquyen->find('list',array('recursive'=>0,'conditions'=>array('role_id'=>$role_id),'fields'=>array('User.username')));
		
		$sinhvien_id = $this->Tgiachidoan->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Sinhvien.mssv'=>$maso),'fields'=>array('Tgiachidoan.sinhvien_id')));
		$chucvus = $this->Chucvusv->find('list',array('recursive'=>0,
														'conditions'=>array('sinhvien_id'=>$sinhvien_id,'ngayketthuc'=>null),
														'fields'=>array('Sinhvien.mssv','Chucv.tenchucvu')));
		
		$sinhviens = $this->Tgiachidoan->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Sinhvien.mssv'=>$maso)));
		$chvus = $this->Chucv->find('list',array('fields'=>array('Chucv.tenchucvu')));
		$role = $this->Role->find('first',array('conditions'=>array('id'=>$role_id)));
		$this->set(compact('sinhviens','chucvus','role','role_id','chvus'));
	}
	
	function updateuser($role_id=null)
	{
		$this->layout='ajax';
		if(!$role_id)
			$this->redirect(array('controller'=>'roles','action'=>'index'));
			
		$loaidv =$this->data['Phanquyen']['loaidv'];
		$khoa_id = $this->Session->read('khoa_id'); 
		if($loaidv==1)
		{
			$role = $this->Role->find('first',array('id'=>$role_id));
			$maso = $this->Phanquyen->find('list',array('recursive'=>0,'conditions'=>array('role_id'=>$role_id),'fields'=>array('User.username')));
			$canbo_id = $this->Tgiachidoancb->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Canbo.mscb'=>$maso),'fields'=>array('Tgiachidoancb.canbo_id')));
			$chucvus = $this->Chucvucb->find('list',array('recursive'=>0,
															'conditions'=>array('canbo_id'=>$canbo_id,'ngayketthuc'=>null),
															'fields'=>array('Canbo.mscb','Chucv.tenchucvu')));
			$doanviens = $this->Tgiachidoancb->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Canbo.mscb'=>$maso)));
		}
		else
		{
			$role = $this->Role->find('first',array('id'=>$role_id));
			$maso = $this->Phanquyen->find('list',array('recursive'=>0,'conditions'=>array('role_id'=>$role_id),'fields'=>array('User.username')));
			$sinhvien_id = $this->Tgiachidoan->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Sinhvien.mssv'=>$maso),'fields'=>array('Tgiachidoan.sinhvien_id')));
			$chucvus = $this->Chucvusv->find('list',array('recursive'=>0,
															'conditions'=>array('sinhvien_id'=>$sinhvien_id,'ngayketthuc'=>null),
															'fields'=>array('Sinhvien.mssv','Chucv.tenchucvu')));
			$doanviens = $this->Tgiachidoan->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'Chidoansv.khoa_id'=>$khoa_id,'Chidoansv.khoa_id'=>$khoa_id,'Sinhvien.mssv'=>$maso)));
			
		}
		$this->set(compact('doanviens','chucvus','role','role_id','loaidv'));
	}
	
	function updateuser_add($role_id=null)
	{
		$this->layout='ajax';
		if(!$role_id)
			$this->redirect(array('controller'=>'roles','action'=>'index'));

		$loaius = $this->data['Phanquyen']['loaius'];
		$chucvu = $this->data['Phanquyen']['chucvu'];
		if(!empty($chucvu))
		{
			$userdc = $this->Phanquyen->find('list',array('conditions'=>array('role_id'=>$role_id),'fields'=>array('user_id')));
				$masodc = $this->User->find('list',array('conditions'=>array('id'=>$userdc),'fields'=>array('username')));
			if($loaius==1)
			{
				$canbo_id =  $this->Chucvucb->find('list',array('recursive'=>0,'conditions'=>array('chucv_id'=>$chucvu,'ngayketthuc'=>null,'NOT'=>array('mscb'=>$masodc)),'fields'=>array('canbo_id')));
				$doanviens = $this->Tgiachidoancb->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'canbo_id'=>$canbo_id)));
			}
			else
			{
				$sinhvien_id =  $this->Chucvusv->find('list',array('recursive'=>0,'conditions'=>array('chucv_id'=>$chucvu,'ngayketthuc'=>null,'NOT'=>array('mssv'=>$masodc)),'fields'=>array('sinhvien_id')));
				$doanviens = $this->Tgiachidoan->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>null,'sinhvien_id'=>$sinhvien_id)));
			}
		}
		$this->set(compact('doanviens','loaius'));
	}
	
	function xoaquyen($role_id=null)
	{
		if(!$role_id)
			$this->redirect(array('controller'=>'roles','action'=>'index'));
		$doanviens = $this->data['Phanquyen']['doanvien'];
		$err=0;
		foreach ($doanviens as $maso=>$value)
		{
			if($value==1)
			{
				$user = $this->User->find('first',array('conditions'=>array('username'=>$maso)));
				if(!$this->Phanquyen->deleteAll(array('role_id'=>$role_id,'user_id'=>$user['User']['id'])))
				{$err=1;}
			}
		}
		if($err==1)
			$this->Session->setFlash('Lỗi khi lưu','error');
		else 
			$this->Session->setFlash('Xóa quyền thành công','success');
		
		$this->redirect(array('action'=>'dsuser',$role_id));
	}

	function phanquyencd()
	{
		if(!empty($this->data))
		{
			$err=0;
			foreach ($this->data['Phanquyen']['quyen'] as $maso=>$data)
			{
				foreach ($data as $role_id=>$val)
				{
					if($val==1)
					{
						$user = $this->User->find('first',array('conditions'=>array('username'=>$maso)));
						$record['Phanquyen']['role_id']=$role_id;						
						$record['Phanquyen']['user_id']=$user['User']['id'];
						if(!$this->Phanquyen->saveAll($record))
						{
							$err=1;
						}
					}
				}
			}
			if($err==0)
				$this->Session->setFlash('Lưu thành công','success');
			else
				$this->Session->setFlash('Lỗi khi Lưu. Vui lòng thử lại.','error');
			$this->redirect(array('action'=>'phanquyencd'));
		}
		$khoa_id = $this->Session->read('khoa_id');
		$querystring= "select Chidoansv.id,Chidoansv.tenchidoan
    					from tgiachidoans as Tgiachidoan,chidoansvs Chidoansv, (select a.chidoansv_id, count(sinhvien_id) as sl from tgiachidoans a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoan1
    					where Tgiachidoan.chidoansv_id = Tgiachidoan1.chidoansv_id 
    							and Tgiachidoan1.sl>0 
    							and Chidoansv.id=Tgiachidoan.chidoansv_id 
    							and ngaychuyen is NULL
    							and Chidoansv.khoa_id= ".$khoa_id."
    						group by Tgiachidoan.chidoansv_id";
    	$laycd_act = $this->Phanquyen->query($querystring);
    	$chidoans =array();
    	foreach ($laycd_act as $chdoan) 
    	{
    		$chidoans[$chdoan['Chidoansv']['id']]=$chdoan['Chidoansv']['tenchidoan'];
    	}
    	$quyens =$this->Role->find('list',array('fields'=>array('rolename')));
    	$this->set(compact('chidoans','quyens'));
	}
	
	function updatecd()
	{
		$this->layout='ajax';
		$khoa_id = $this->Session->read('khoa_id');
		$loaidv = isset($this->data['Phanquyen']['loaidv'])?$this->data['Phanquyen']['loaidv']:$this->data['Phanquyen']['loaidvpq'];
		$tenchidoan = isset($this->data['Phanquyen']['loaidv'])?'chidoan':'chidoanpq';
		if($loaidv==0)
		{
			$querystring= "select Chidoansv.id,Chidoansv.tenchidoan
	    					from tgiachidoans as Tgiachidoan,chidoansvs Chidoansv, (select a.chidoansv_id, count(sinhvien_id) as sl from tgiachidoans a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoan1
	    					where Tgiachidoan.chidoansv_id = Tgiachidoan1.chidoansv_id 
	    							and Tgiachidoan1.sl>0 
	    							ad ngaychuyen is NULL
	    							and Chidoansv.id=Tgiachidoan.chidoansv_id 
	    							and Chidoansv.khoa_id= ".$khoa_id."
	    						group by Tgiachidoan.chidoansv_id";
		}
		else
		{
			$querystring= "select Chidoansv.id,Chidoansv.tenchidoan
	    					from tgiachidoancbs as Tgiachidoancb,chidoansvs Chidoansv, (select a.chidoansv_id, count(canbo_id) as sl from tgiachidoancbs a where ngaychuyen is NULL group by a.chidoansv_id) as Tgiachidoancb1
	    					where Tgiachidoancb.chidoansv_id = Tgiachidoancb1.chidoansv_id 
	    							and Tgiachidoancb1.sl>0 
	    							and ngaychuyen is NULL
	    							and Chidoansv.id=Tgiachidoancb.chidoansv_id 
	    							and Chidoansv.khoa_id= ".$khoa_id."
	    						group by Tgiachidoancb.chidoansv_id";
		}
		
		$laycd_act = $this->Phanquyen->query($querystring);
	    $chidoans =array();
	    foreach ($laycd_act as $chdoan) 
	    {
	    	$chidoans[$chdoan['Chidoansv']['id']]=$chdoan['Chidoansv']['tenchidoan'];
	    }
    	$this->set(compact('chidoans','tenchidoan'));
	}
	
	function updateds()
	{
		$this->layout='ajax';
		$loaidv = $this->data['Phanquyen']['loaidv'];
		$chidoan = $this->data['Phanquyen']['chidoan'];
		if(!empty($chidoan))
		{
			$quyens =$this->Role->find('list',array('fields'=>array('rolename')));
			if($loaidv==0)
			{
				$dvcocv = $this->Chucvusv->find('list',array('fields'=>array('sinhvien_id')));
				$dv = $this->Tgiachidoan->find('list',array('conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan,'sinhvien_id'=>$dvcocv),'fields'=>array('sinhvien_id')));
				$doanviens = $this->Tgiachidoan->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'sinhvien_id'=>$dv)));
				$chucvus = $this->Chucvusv->find('list',array('recursive'=>0,'conditions'=>array('sinhvien_id'=>$dv),'fields'=>array('Sinhvien.mssv','Chucv.tenchucvu')));
				$madv = $this->Tgiachidoan->Sinhvien->find('list',array('conditions'=>array('id'=>$dv),'fields'=>array('Sinhvien.mssv')));
				$tenloaidv ='Sinhvien';
				$maso ='mssv';
			}
			else
			{
				$dvcocv = $this->Chucvucb->find('list',array('fields'=>array('canbo_id')));
				$dv = $this->Tgiachidoancb->find('list',array('conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan,'canbo_id'=>$dvcocv),'fields'=>array('canbo_id')));
				$doanviens = $this->Tgiachidoancb->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'canbo_id'=>$dv)));
				$chucvus = $this->Chucvucb->find('list',array('recursive'=>0,'conditions'=>array('canbo_id'=>$dv),'fields'=>array('Canbo.mscb','Chucv.tenchucvu')));
				$madv = $this->Tgiachidoancb->Canbo->find('list',array('conditions'=>array('id'=>$dv),'fields'=>array('Canbo.mscb')));
				$tenloaidv ='Canbo';
				$maso ='mscb';
			}
			
			$qdv = $this->Phanquyen->find('all',array('recursive'=>0,'conditions'=>array('User.username'=>$madv),'fields'=>array('User.username','Role.rolename')));
			$quyendvs =array();
			foreach ($doanviens as $doanvien)
			{
				foreach ($qdv as $quyen)
				{
					if($doanvien[$tenloaidv][$maso]==$quyen['User']['username'])
					{
						$quyendvs[$doanvien[$tenloaidv][$maso]][]=$quyen['Role']['rolename'];
					}
				}
			}
			$this->set(compact('chucvus','doanviens','loaidv','quyens','tenloaidv','maso','quyendvs'));
		}
	}

	function updatedspquyen()
	{
		$this->layout='ajax';
		$chidoan = $this->data['Phanquyen']['chidoanpq'];
		$loaidv = $this->data['Phanquyen']['loaidvpq'];
		if(!empty($chidoan))
		{
			$quyens =$this->Role->find('list',array('fields'=>array('rolename')));
			if($loaidv==0)
			{
				$dv = $this->Tgiachidoan->find('list',array('recirsive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan),'fields'=>array('sinhvien_id'))); 
				$doanviens = $this->Tgiachidoan->find('all',array('recirsive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan)));
				$chucvudv = $this->Chucvusv->find('list',array('recursive'=>0,'conditions'=>array('sinhvien_id'=>$dv),'fields'=>array('Sinhvien.mssv','Chucv.tenchucvu')));
				$madv = $this->Tgiachidoan->Sinhvien->find('list',array('conditions'=>array('id'=>$dv),'fields'=>array('Sinhvien.mssv')));
				$tenloaidv ='Sinhvien';
				$maso ='mssv';
				
			}
			else
			{
				$dv = $this->Tgiachidoancb->find('list',array('recirsive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan),'fields'=>array('canbo_id')));
				$doanviens = $this->Tgiachidoancb->find('all',array('recirsive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan)));
				$chucvudv = $this->Chucvucb->find('list',array('recursive'=>0,'conditions'=>array('canbo_id'=>$dv),'fields'=>array('canbo_id','Chucv.tenchucvu')));
				$madv = $this->Tgiachidoancb->Canbo->find('list',array('conditions'=>array('id'=>$dv),'fields'=>array('Canbo.mscb')));
				$tenloaidv ='Canbo';
				$maso ='mscb';
			}
			
			$quyendv = $this->Phanquyen->find('all',array('recursive'=>0,'conditions'=>array('User.username'=>$madv)));
			$quyendvs =array();
			foreach ($doanviens as $doanvien)
			{
				foreach ($quyendv as $quyen)
				{
					if($doanvien[$tenloaidv][$maso]==$quyen['User']['username'])
					{
						$quyendvs[$doanvien[$tenloaidv][$maso]][]=$quyen['Role']['id'];
					}
				}
			}
		}
		$this->set(compact('doanviens','quyendvs','quyens','tenloaidv','maso','chucvudv'));
	}
	
	function xoaq($maso=null, $role_id=null)
	{
		if(!$maso || !$role_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		$user = $this->User->find('first',array('conditions'=>array('username'=>$maso)));
		if($this->Phanquyen->deleteAll(array('user_id'=>$user['User']['id'],'role_id'=>$role_id)))
		{
			$this->Session->setFlash('Xóa quyền thành công','success');
		}
		else
		{
			$this->Session->setFlash('Lỗi khi xóa. Vui lòng thử lại.','error');
		}
		$this->redirect(array('action'=>'phanquyencd'));
	}

	function phanquyenchucd()
	{
		if(!empty($this->data))
		{
			$khoa_id = $this->Session->read('khoa_id');
			$thaotac = $this->data['Phanquyen']['action'];
			foreach ($this->data['Phanquyen']['checkbox'] as $chucvu_id=>$qs)
			{
				if($this->data['Phanquyen']['loaidvadd']==0)
				{
					$dvs = $this->Tgiachidoan->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'Chidoansv.khoa_id'=>$khoa_id),
																				'fields'=>array('sinhvien_id')));
					$maso = $this->Chucvusv->find('list',array('recursive'=>0,'conditions'=>array('sinhvien_id'=>$dvs,'chucv_id'=>$chucvu_id),'fields'=>array('Sinhvien.mssv')));
				}
				else
				{
					$dvs = $this->Tgiachidoancb->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'Chidoansv.khoa_id'=>$khoa_id),
																				'fields'=>array('canbo_id')));
					$maso = $this->Chucvucb->find('list',array('recursive'=>0,'conditions'=>array('canbo_id'=>$dvs,'chucv_id'=>$chucvu_id),'fields'=>array('Canbo.mscb')));
				}
				$user_ids = $this->User->find('list',array('conditions'=>array('username'=>$maso)));
				$err=0;
				foreach ($qs as $qu_id=>$val)
				{
					if($val==1)
					{
						foreach ($user_ids as $user_id)
						{
							$data['Phanquyen']['user_id']=$user_id;
							$data['Phanquyen']['role_id']=$qu_id;
							if($thaotac==0)
							{
								if(!$this->Phanquyen->saveAll($data))
								{
									$err=1;
								}
							}
							else
							{
								if(!$this->Phanquyen->deleteAll(array('role_id'=>$qu_id,'user_id'=>$user_id)))
								{
									$err=1;
								}
							}
						}
					}
				}
			}
			if($thaotac==0)
			{
				if($err==0)
					$this->Session->setFlash('Lưu thành công','success');
				else
					$this->Session->setFlash('Lỗi khi lưu. vui lòng thử lại','error');
			}	
			else
			{
				if($err==0)
					$this->Session->setFlash('Xóa thành công','success');
				else
					$this->Session->setFlash('Lỗi khi xóa. vui lòng thử lại','error');
			}
			$this->redirect(array('action'=>'phanquyenchucd'));
		}
		$chucvus = $this->Chucv->find('list',array('fields'=>array('tenchucvu')));
		$quyens =$this->Role->find('list',array('fields'=>array('rolename')));
		$this->set(compact('chucvus','quyens'));
	}

	function updatedspqchucdanh()
	{
		$this->layout='ajax';
		$loaidv = $this->data['Phanquyen']['loaidv'];
		$chucvu = $this->data['Phanquyen']['chucvu'];
		$khoa_id = $this->Session->read('khoa_id');
		if(!empty($chucvu))
		{
			$quyens =$this->Role->find('list',array('fields'=>array('rolename')));
			if($loaidv==0)
			{
				$dsdv = $this->Chucvusv->find('list',array('conditions'=>array('chucv_id'=>$chucvu),'fields'=>array('sinhvien_id')));
				$dvs = $this->Tgiachidoan->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'sinhvien_id'=>$dsdv,'Chidoansv.khoa_id'=>$khoa_id),'fields'=>array('Sinhvien.mssv')));
				$doanviens = $this->Tgiachidoan->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'sinhvien_id'=>$dsdv,'Chidoansv.khoa_id'=>$khoa_id)));
				$qdvs = $this->Phanquyen->find('all',array('recursive'=>0,'conditions'=>array('User.username'=>$dvs)));
				$tenloaidv ='Sinhvien';
				$maso ='mssv';
			}
			else
			{
				$dsdv = $this->Chucvucb->find('list',array('conditions'=>array('chucv_id'=>$chucvu),'fields'=>array('canbo_id')));
				$dvs = $this->Tgiachidoancb->find('list',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'canbo_id'=>$dsdv,'Chidoansv.khoa_id'=>$khoa_id),'fields'=>array('Canbo.mscb')));
				$doanviens = $this->Tgiachidoancb->find('all',array('recursive'=>0,'conditions'=>array('ngaychuyen'=>NULL,'canbo_id'=>$dsdv,'Chidoansv.khoa_id'=>$khoa_id)));
				$qdvs = $this->Phanquyen->find('all',array('recursive'=>0,'conditions'=>array('User.username'=>$dvs)));
				$tenloaidv ='Canbo';
				$maso ='mscb';
			}
			$quyendvs =array();
			foreach ($doanviens as $doanvien)
			{
				foreach ($qdvs as $quyen)
				{
					if($doanvien[$tenloaidv][$maso]==$quyen['User']['username'])
					{
						$quyendvs[$doanvien[$tenloaidv][$maso]][]=$quyen['Role']['rolename'];
					}
				}
			}
		}
		//debug($doanviens);
		//debug($quyendvs);
		$this->set(compact('doanviens','quyendvs','tenloaidv','quyens','maso'));
	}

}
